package cn.qyd.javareflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author 邱运铎
 * @date 2024-03-03 23:32
 */
public class ReflectDemo {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchFieldException {
        Class write = Class.forName("cn.qyd.javareflect.Write");
        Constructor constructor = write.getConstructor();
        Object object = constructor.newInstance();
        Method getName = write.getMethod("getName");
        Field nameField = write.getField("name");
        System.out.println(nameField.get(object));
        String name = (String) getName.invoke(object);
        System.out.println(name);
    }
}
